package com.bmob.lover.ui.fragment.user

import android.os.Bundle
import android.view.Gravity
import android.view.View
import com.theone.common.constant.BundleConstant
import com.theone.common.ext.getValueNonNull
import com.theone.common.ext.startActivity
import com.bmob.lover.R
import com.bmob.lover.app.ext.isTestAdmin
import com.bmob.lover.data.model.enums.TYPE_MODIFY_PASSWORD
import com.bmob.lover.databinding.FragmentResetPasswordBinding
import com.bmob.lover.ui.activity.LoginRegisterActivity
import com.bmob.lover.ui.fragment.base.BaseFragment
import com.bmob.lover.viewmodel.ResetPasswordViewModel
import com.theone.mvvm.ext.qmui.setTitleWitchBackBtn
import com.theone.mvvm.ext.qmui.showFailTipsDialog
import com.theone.mvvm.ext.qmui.showSuccessTipsDialog

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 * @author The one
 * @date 2021-05-14 15:09
 * @describe TODO
 * @email 625805189@qq.com
 * @remark
 */
class ResetPasswordFragment private constructor() :
    BaseFragment<ResetPasswordViewModel, FragmentResetPasswordBinding>() {

    companion object {
        fun newInstance(phone: String,type:Int): ResetPasswordFragment {
            return ResetPasswordFragment().apply {
                arguments = Bundle().apply {
                    putString(BundleConstant.DATA, phone)
                    putInt(BundleConstant.TYPE, type)
                }
            }
        }
    }

    private val mPhone: String by getValueNonNull(BundleConstant.DATA)
    private val mType: Int by getValueNonNull(BundleConstant.TYPE)

    override fun initView(root: View) {
        setTitleWitchBackBtn(R.string.reset_password)
        getTopBar()?.setTitleGravity(Gravity.START)
    }

    override fun initData() {
        getViewModel().phoneNumber.set(mPhone)
    }

    override fun createObserver() {
        getViewModel().run {
            getResponseLiveData().observe(this@ResetPasswordFragment) {
                showSuccessTipsDialog("重置成功"){
                    if(mType == TYPE_MODIFY_PASSWORD){
                        finish()
                    }else{
                        startActivity(LoginRegisterActivity::class.java,true)
                    }
                }
            }
            getErrorLiveData().observe(this@ResetPasswordFragment) {
                showFailTipsDialog(it)
            }
        }
    }

    override fun getBindingClick() = ProxyClick()

    inner class ProxyClick  {

        fun reset() {
            with(getViewModel()) {
                when {
                    code.get().isEmpty() -> showFailTipsDialog("请填写验证码")
                    password.get().isEmpty() -> showFailTipsDialog("请填写密码")
                    rePassword.get().isEmpty() -> showFailTipsDialog("请填写确认密码")
                    password.get() != rePassword.get() -> showFailTipsDialog("两次密码输入不相同")
                    else -> {
                        if(mAppVm.getUserInfoLiveData().value.isTestAdmin()){
                            return
                        }
                        requestServer()
                    }
                }
            }
        }

    }

}